#include "settime.h"

SetTime::SetTime(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
     this->setFixedSize(640,430);
     ui_Init();
     connect_Init();
}
void SetTime::ui_Init()
{

    for(int i=0;i<3;i++)//创建主屏幕显示的按键
    {
         theLabel[i] = new QLabel(this);
    }
    theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[0]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[0]->setText("选择日期 ");



   theDateTimeEdit = new QDateTimeEdit(this);
   theDateTimeEdit->setDisplayFormat("yyyy_MM_dd hh:mm");
   theDateTimeEdit->setDateTime(QDateTime::currentDateTime());
   theDateTimeEdit->stepUp();
   theDateTimeEdit->stepDown();

timestyle=("color:white;\
background-color: rgb(0, 170, 255);\
 border-radius:4px;  border: 1px groove white; padding:1px 4px;\
border-style: outset;\
/* 按钮文本设置 */\
text-align: content;			             /* 文本的位置 */\
font-size: 26px;\
color: #FFFFFF;			                 /* 文本颜色 */\
");
   theDateTimeEdit->setGeometry(170,150,300,60 );//设定每隔放置一个按键
   theDateTimeEdit->setStyleSheet(timestyle);
theDateTimeEdit->setAlignment(Qt::AlignHCenter);
    for(int i=0;i<1;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
        theBtn[i]->setFixedSize(200,40);
      btnlistUi.append(theBtn[i]);
}
    theBtn[0]->setStyleSheet(  "QPushButton{"+theBox_style[0]+ "background-image: url(:/pic/切.png); /* 背景图片 */"+"}"
     "QPushButton:focus{"+theBox_style[1]+ "background-image: url(:/pic/切.png); /* 背景图片 */"+"}");

btnlistUi.at(0)->setText(tr("确定"));
btnlistUi.at(0)->setFocus();
btnlistUi.at(0)->setGeometry(170,300,100,50 );//设定每隔放置一个按键



//事件过滤器
foreach (QPushButton *btn, btnlistUi) {
    btn->installEventFilter(this);
}
}
void SetTime::connect_Init()
{
    //查看记录
    connect(btnlistUi.at(0),&QPushButton::clicked,[=](){
        QString str1 = "date -s " +theDateTimeEdit->date().toString("yyyy-MM-dd");
        QString str2 = "date -s " +theDateTimeEdit->time().toString("hh:mm:ss");
        system(str1.toLatin1().data());
        //str = "date -s " + hour + ":" + min + ":" + "00";
        system(str2.toLatin1().data());    //强制写入到CMOS
        system("hwclock -w");
        this->parentWidget()->show();
        this->close();
    });


}
bool SetTime::eventFilter(QObject *watched, QEvent *event)
{
    static int press_y;
    static int relea_y;
    static int press_x;
    static int relea_x;
        for(int i=0;i<btnlistUi.length();i++)
            if(btnlistUi.at(i) == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case OK:
                 {
                   emit  btnlistUi.at(0)->clicked();
                   break;
                 }
               case UP://上
               {
                   theDateTimeEdit->stepUp();
                   break;
               }
               case DOWN://下
               {
                   theDateTimeEdit->stepDown();
                   break;
               }
               case LEFT://左
               {
                   theDateTimeEdit->setCurrentSectionIndex(theDateTimeEdit->currentSectionIndex()-1);
                   theDateTimeEdit->stepUp();
                   theDateTimeEdit->stepDown();

                 break;
               }
               case RIGHT://右
               {
                   theDateTimeEdit->setCurrentSectionIndex(theDateTimeEdit->currentSectionIndex()+1);
                   theDateTimeEdit->stepUp();
                   theDateTimeEdit->stepDown();

              break;
               }
               case BACK:
               {
                   this->parentWidget()->show();
                   this->close();
                   break;
               }
               case INIT:
               {

                   init_Show *show = new init_Show(this);
                   show->show();
                   this->hide();
                   break;
               }
               default:
                   break;
               }
            }
            }
            else
            {
            QMouseEvent *ev = (QMouseEvent *)event;
            if(ev->type() == QEvent::MouseButtonPress)
            {
                press_x = ev->globalX();
            }
            if(ev->type() == QEvent::MouseButtonRelease)
            {
                relea_x = ev->globalX();
            }

            if(ev->type() == QEvent::MouseButtonPress)
            {
                press_y = ev->globalY();
            }
            if(ev->type() == QEvent::MouseButtonRelease)
            {
                relea_y = ev->globalY();
            }

            if((press_x-relea_x)>20 && ev->type() == QEvent::MouseButtonRelease)
            {
                theDateTimeEdit->setCurrentSectionIndex(theDateTimeEdit->currentSectionIndex()-1);
                theDateTimeEdit->stepUp();
                theDateTimeEdit->stepDown();
            }
            else if((relea_x-press_x)>20 && ev->type() == QEvent::MouseButtonRelease)
            {
                theDateTimeEdit->setCurrentSectionIndex(theDateTimeEdit->currentSectionIndex()+1);
                theDateTimeEdit->stepUp();
                theDateTimeEdit->stepDown();
            }
            else if((press_y-relea_y)>20 && ev->type() == QEvent::MouseButtonRelease)
            {
                theDateTimeEdit->stepUp();
            }
            else if((relea_y-press_y)>20 && ev->type() == QEvent::MouseButtonRelease)
            {
                theDateTimeEdit->stepDown();
            }
            }
        return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}

